<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：商品名称"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input class="w500" type="textarea" v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="关键词" >
        <el-input class="w500" type="textarea" v-model="form.keywords"></el-input>
      </el-form-item>
      <el-form-item label="选择所属应用">
        <el-select v-model="form.app_id" placeholder="请选择">
          <el-option v-for="item in applistall" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="可读的应用">
        <el-checkbox-group v-model="allow_app_read">
          <el-checkbox v-for="i in applistall" :label="i.id" :key="i.id">{{i.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="分类图标">
        <el-upload
          :action="upload_url"
          list-type="picture-card"
          :file-list="fileList"
          :on-success="handleAvatarSuccess">
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input class="w100" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'
  import { UPLOAD_URL } from '@/config/const'

  export default {
    data: function () {
      return {
        fileList: [
          {name: '', url: ''}
        ],
        allow_app_read: [],
        rules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '请输入描述', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      ...mapState({
        applistall: state => state.app.all,
        form: state => state.mallgoodsclass.saveData,
        cover: state => state.mallgoodsclass.cover
      }),
      upload_url () {
        return UPLOAD_URL
      }
    },
    async created () {
      this.fileList[0].name = String(this.form.name)
      this.fileList[0].url = String(this.form.profile_pic)
      await this.getapplistall()
      let arr = this.form.other_app_read.split(',')
      for (let i = 0; i < arr.length; i++) {
        arr[i] = Number(arr[i])
      }
      this.allow_app_read = arr
    },
    methods: {
      // 获取全部应用列表
      async getapplistall () {
        await this.$store.dispatch('app/appAll')
      },
      onSubmit (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            self.form.other_app_read = self.allow_app_read.join(',')
            if (JSON.stringify(self.cover) !== '{}') {
              self.form.profile_pic = 'http://' + self.cover.save_path + '/' + self.cover.filename
            }
            self.$store.dispatch('mallgoodsclass/editGoodsCate', self.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/integral/goodsclass')
            })
          }
        })
      },
      handleAvatarSuccess (file, response, fileList) {
        const self = this
        if (file.code !== 200) {
          fileList.pop()
          notify.warning('封面上传失败')
        } else {
          notify.success('封面上传成功')
        }
        self.$store.commit('mallgoodsclass/INTEGRAL_CLASS_COVER', file.res)
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
